package com.ctakit.kiwi.common.core.util;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

public class MyFileUtils extends FileUtils {
    // private static final SecureRandom random = new SecureRandom();
    private static final String FOLDER_SEPARATOR = "/";

    private static final String EXTENSION_SEPARATOR = ".";

    public static String getFolderSeparator() {
        return FOLDER_SEPARATOR;
    }

    public static String getExtensionSeparator() {
        return EXTENSION_SEPARATOR;
    }

    /***
     * 删除文件或目录
     *
     */
    public static boolean deleteFile(File f) {
        if (f.exists()) {
            if (f.isFile()) {
                return f.delete();
            } else if (f.isDirectory()) {
                File[] files = f.listFiles();
                for (File file : files != null ? files : new File[0]) {
                    if (!deleteFile(file))
                        return false;
                }
                return f.delete();
            } else
                return false;
        } else
            return false;
    }

    /**
     * 生成随机的不重复的文件名，
     *
     * @param ext 后缀名
     * @return 返回结果
     */
    public static String createtFileNameByDateTime(String ext) {
        // 转换为字符串
        String formatDate = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
        // 随机生成文件编号
        int random = new Random().nextInt(10000);
        String stringBuffer;
        stringBuffer = formatDate +
                random +
                ext;
        return stringBuffer;
    }

    /**
     * 生成随机的不重复的文件名，
     *
     * @param ext 扩展名
     * @return 返回值
     */
    public static String createtFileName(String ext) {
        return IdUtil.fastUUID() + EXTENSION_SEPARATOR + ext;
    }

    /**
     * 生成随机(UUID)的不重复的文件名，根据日期生成对应的目录
     *
     * @param ext 文件的扩展名
     */
    public static String createtFileByDate(String ext) {
        Calendar calendar = Calendar.getInstance();
        return MyFileUtils.FOLDER_SEPARATOR + getDayDir(calendar) + MyFileUtils.FOLDER_SEPARATOR + getTimeDir(calendar) + MyFileUtils.FOLDER_SEPARATOR + createtFileName(ext);
    }

    /**
     * 返回某天的目录 例如: 20100401
     *
     * @param cl 日期
     * @return 返回文件名
     */
    private static String getDayDir(Calendar cl) {
        String ys, ms;
        ys = cl.get(Calendar.YEAR) + "";
        int month = cl.get(Calendar.MONTH) + 1;
        if (month < 10) {
            ms = "0" + month;
        } else {
            ms = month + "";
        }
        int day = cl.get(Calendar.DATE);
        if (day < 10) {
            return ys + ms + "0" + day;
        } else {
            return ys + ms + day;
        }
    }

    /**
     * 返回当前时刻的目录 每时分钟生成一个新的目录，例如: 1900, 1910, 1920，代表19点0～9， 19点10~19分
     *
     * @param cl 日期
     * @return 返回文件名
     */
    private static String getTimeDir(Calendar cl) {
        String timeDir;
        if (cl.get(Calendar.HOUR_OF_DAY) < 10) {
            timeDir = "0" + cl.get(Calendar.HOUR_OF_DAY);
        } else {
            timeDir = cl.get(Calendar.HOUR_OF_DAY) + "";
        }
        int min = cl.get(Calendar.MINUTE);
        switch (min / 10) {
            case 0:
                timeDir = timeDir + "00";
                break;
            case 1:
                timeDir = timeDir + "10";
                break;
            case 2:
                timeDir = timeDir + "20";
                break;
            case 3:
                timeDir = timeDir + "30";
                break;
            case 4:
                timeDir = timeDir + "40";
                break;
            case 5:
                timeDir = timeDir + "50";
                break;
        }
        return timeDir;
    }

    /**
     * Extract the filename extension from the given path, e.g.
     * "mypath/myfile.txt" -> "txt".
     *
     * @param path the file path (may be <code>null</code>)
     * @return the extracted filename extension, or <code>null</code> if none
     */
    public static String getFileNameExtension(String path) {
        return getFileNameExtension(path, null);
    }

    /**
     * Extract the filename extension from the given path, e.g.
     * "mypath/myfile.txt" -> "txt".
     *
     * @param path        the file path (may be <code>null</code>)
     * @param defultValue the file path (may be <code>null</code>)
     * @return the extracted filename extension, or <code>null</code> if none
     */
    public static String getFileNameExtension(String path, String defultValue) {
        if (path == null) {
            return defultValue;
        }
        int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR);
        if (extIndex == -1) {
            return defultValue;
        }
        int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR);
        if (folderIndex > extIndex) {
            return defultValue;
        }
        return path.substring(extIndex + 1);
    }

    /**
     * Extract the filename extension from the given path, e.g.
     * "mypath/myfile.txt" -> ".txt".
     *
     * @param path the file path (may be <code>null</code>)
     * @return the extracted filename extension, or <code>null</code> if none
     */
    public static String getFileNameFullExtension(String path) {
        return getFileNameFullExtension(path, null);
    }

    /**
     * Extract the filename extension from the given path, e.g.
     * "mypath/myfile.txt" -> ".txt".
     *
     * @param path        the file path (may be <code>null</code>)
     * @param defultValue the file path (may be <code>null</code>)
     * @return the extracted filename extension, or <code>null</code> if none
     */
    public static String getFileNameFullExtension(String path, String defultValue) {
        if (path == null) {
            return defultValue;
        }
        int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR);
        if (extIndex == -1) {
            return defultValue;
        }
        int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR);
        if (folderIndex > extIndex) {
            return defultValue;
        }
        return path.substring(extIndex);
    }

    /**
     * 得到文件名 （除去扩展名）
     *
     * @param path 路径名
     * @return 文件名
     */
    public static String getFileNameNoExtension(String path) {
        if (path == null) {
            return null;
        }
        int extIndex = path.lastIndexOf(EXTENSION_SEPARATOR);
        if (extIndex == -1) {
            return path;
        }
        int folderIndex = path.lastIndexOf(FOLDER_SEPARATOR);
        if (folderIndex > extIndex) {
            return path;
        }
        return path.substring(0, extIndex);
    }

    public static void main(String[] args) {
        System.out.println(getFileNameExtension("/a/.b/c"));
    }
}
